package com.mashibing.controller;

import com.msb.framework.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName:CacheController
 * @author:[开发者名称]
 * @Description:[类的功能描述]
 * @Date:2025/9/16/10:37
 * @Version:1.0
 */
@RestController
@Slf4j
public class CacheController {


        @Autowired
        private RedisClient redisClient;

        @PostMapping(value = "/cache/hmset/{key}")
        public void hmset(@PathVariable(value = "key")String key, @RequestBody Map<String,Object> map){
            log.info("【缓存模块】 hmset方法，存储key = {}，存储value = {}",key,map);
            redisClient.hSet(key,map);//向redis添加哈希数据类型
        }

        @PostMapping(value = "/cache/set/{key}")
        public void set(@PathVariable(value = "key")String key, @RequestParam(value = "value")String value){
            log.info("【缓存模块】 set方法，存储key = {}，存储value = {}",key,value);//向redis添加缓存set数据类型
            redisClient.set(key,value);
        }
    @PostMapping(value = "/cache/sadd/{key}")
    void sadd(@PathVariable(value = "key")String key, @RequestBody Map<String,Object>... value){
        log.info("【缓存模块】 sadd方法，存储key = {}，存储value = {}", key, value);
        redisClient.sAdd(key,value);
    }

    @GetMapping("/cache/hgetall/{key}")
    Map hGetAll(@PathVariable(value = "key")String key){
        log.info("【缓存模块】 hGetAll方法，获取key ={} 的数据", key);
        Map<String, Object> value = redisClient.hGetAll(key);
        log.info("【缓存模块】 hGetAll方法，获取key ={} 的数据 value = {}", key,value);
        return value;

    }

    /**
     *
     * @param key  缓存hash数据集类型 hashkry
     * @param field  hash  key
     * @return  查询  通过hashkey key    查询value
     */
    @GetMapping("/cache/hget/{key}/{field}")
    public Object hget(@PathVariable(value = "key")String key,@PathVariable(value = "field")String field){
        log.info("【缓存模块】 hget方法，获取key ={}，field = {}的数据", key,field);
        Object value = redisClient.hGet(key, field);
        log.info("【缓存模块】 hGetAll方法，获取key ={}，field = {} 的数据 value = {}", key,field,value);
        return value;
    }
    @GetMapping("/cache/smember/{key}")
    public Set smember(@PathVariable(value = "key")String key){
        log.info("【缓存模块】 smember方法，获取key ={}的数据", key);
        Set<Object> values = redisClient.sMembers(key);
        log.info("【缓存模块】 smember方法，获取key ={} 的数据 value = {}", key,values);
        return values;
    }
}

